package com.chat.view;

import com.chat.Local;
import com.chat.service.AddCommonPharse;
import com.chat.service.CommonPharse;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.IOException;


public class CommonPharseController {

    public ListView commonlist;

    public TextField commonTextField;

    public static ObservableList<CommonListData> com= FXCollections.observableArrayList();

    public void initialize(){
       commonlist.getItems().clear();
       com.clear();
       commonlist.setItems(com);

        CommonPharse getCommonPharse=new CommonPharse();
        getCommonPharse.get(Local.user.getUsername());

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i=0;i<Local.commonlist.size();i++){
                com.add(new CommonListData(Local.commonlist.get(i)));
        }

        commonlist.setCellFactory(new Callback<ListView, ListCell>() {
            @Override
            public ListCell call(ListView param) {
                return new ListCell<CommonListData>(){
                    @Override
                    protected void updateItem(CommonListData item, boolean empty){
                        super.updateItem(item,empty);
                        if(!empty){
                            HBox hbox=new HBox();
                            FXMLLoader fxmlLoader=new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("commonlist.fxml"));
                            try {
                                hbox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            setOnMouseClicked(event ->{
                            });
                            CommonListController commonListController=fxmlLoader.getController();
                            hbox.setPadding(new Insets(20,0,0,0));
                            hbox.setPrefHeight(30);
                           commonListController.setCommonlang(item.getCommonlang());
                            Platform.runLater(()-> {
                                this.setGraphic(hbox);
                            });
                        }
                        else {
                            Platform.runLater(()-> {
                                this.setGraphic(null);
                            });

                        }
                    }
                };
            }
        });

    }
    public void add(MouseEvent mouseEvent) {
         if(commonTextField.getText().length()==0) {
             Alert alert = new Alert(Alert.AlertType.INFORMATION);
             alert.setTitle("提示");
             alert.setHeaderText("警告");
             alert.setContentText("不能添加空！");
             alert.showAndWait();
         }else{
             Alert alert = new Alert(Alert.AlertType.INFORMATION);
             alert.setTitle("提示");
             alert.setHeaderText("恭喜");
             alert.setContentText("添加成功！");
             alert.showAndWait();
             com.add(new CommonListData(commonTextField.getText()));
             AddCommonPharse addCommonPharse=new AddCommonPharse();
             addCommonPharse.add(commonTextField.getText(),Local.user.getUsername());
             commonTextField.setText("");

         }
    }
    public void return1(){
                Stage stage=(Stage) commonlist.getScene().getWindow();
                stage.close();
        try {
            MainWindowView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }//返回按钮的点击事件
}
